package C046;

import java.util.HashMap;
import java.util.Scanner;

/**
 * ClassName: C04
 * Package: C046
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/2/8 10:07
 * @Version 1.0
 * @Since 1.0
 */
public class C04 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scan.nextInt();
        }
        System.out.println(process(arr));
    }

    private static int process(int[] arr) {
        // 正数变1，负数变-1
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] > 0) {
                arr[i] = 1;
            } else if(arr[i] < 0){
                arr[i] = -1;
            }
        }

        // 前缀和
        int[] sum = getSumArr(arr);

        // 生成map数组
        int res = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < sum.length; i++) {
            if(!map.containsKey(sum[i])) {
                map.put(sum[i], i);
            } else {
                res = Math.max(res, i - map.get(sum[i]));
            }
        }
        return res;
    }

    private static int[] getSumArr(int[] arr) {
        int[] res = new int[arr.length];
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
            res[i] = sum;
        }
        return res;
    }
}
